#!/bin/bash

# 函数：创建目录（如果不存在）
create_directory_if_not_exists() {
  local dir="$1"

  if [ ! -d "$dir" ]; then
    echo "Directory does not exist, creating it: $dir"
    mkdir -p "$dir"
  fi
}

# 函数：复制材料文件夹
copy_materials() {
  local source_dir="$1"
  local dest_dir="$2"

  create_directory_if_not_exists "$source_dir"
  create_directory_if_not_exists "$dest_dir"

  # 使用rsync同步材料文件夹，-a保留属性，--delete删除目标中源没有的文件
  rsync -a --delete "$source_dir/" "$dest_dir/"
}

# 函数：复制构建配置文件
copy_build_profile() {
  local source_file="$1"
  local target_file="$2"

  # 确保目标文件所在的目录存在
  create_directory_if_not_exists "$(dirname "$target_file")"

  # 复制构建配置文件
  cp "$source_file" "$target_file"
}

# 提示用户选择模式
echo "请选择当前模式::"
echo "1.调试模式 Debug mode"
echo "2.发布模式 Release mode"
read -p "Enter your choice (1 or 2): " MODE

# 验证用户输入
if [ "$MODE" != "1" ] && [ "$MODE" != "2" ]; then
  echo "Invalid choice. Please enter 1 for Debug mode or 2 for Release mode."
  exit 1
fi

SOURCE_JSON="build-profile.json5"
WIN_BUILD_DIR="build_opt/win"
MAC_BUILD_DIR="build_opt/mac"

if [ "$MODE" == "1" ]; then
  echo "Copying files for debug mode..."
  copy_build_profile "$SOURCE_JSON" "$WIN_BUILD_DIR/build-profile-debug.json5"
  copy_build_profile "$SOURCE_JSON" "$MAC_BUILD_DIR/build-profile-debug.json5"
  copy_materials "$MAC_BUILD_DIR/debug/material" "$WIN_BUILD_DIR/debug/material"
elif [ "$MODE" == "2" ]; then
  echo "Copying files for release mode..."
  copy_build_profile "$SOURCE_JSON" "$WIN_BUILD_DIR/build-profile-release.json5"
  copy_build_profile "$SOURCE_JSON" "$MAC_BUILD_DIR/build-profile-release.json5"
  copy_materials "$MAC_BUILD_DIR/release/material" "$WIN_BUILD_DIR/release/material"
fi

echo "Done."